<template>
	<view :class="is_ipX?'foot_bottom':''">
		<view>
			<view class="cu-bar solid-bottom" >
				<view class="action">
					<image :src="tabCur==0?expend[activeIndex].url1:income[incomeActiveIndex].url1" class="image" />
					<text style="color:#1296db;" class="icon-item-text">{{ tabCur==0?expend[activeIndex].text:income[incomeActiveIndex].text }}</text>
					<view class="solid-bottom padding" style="position: absolute;right: 0;">
						<text class="text-price">{{ tabCur==0?money:incomeMoney}}</text>
					</view>
				</view>
			</view>
			<view class="list row" >
				<view class="item" v-for="(item,index) in tabCur==0?expend:income" :key="index">
					<view class="grid-item-box" @click="switchActive(index)">
						<!-- <uni-icons :type="item.name" :color="activeIndex === index?'#007aff':'#8f8f94'" size="25" /> -->
						<image v-if="tabCur==0" :style="activeIndex === index?'background:'+col:''" :src="item.url" class="image" />
						<!-- <image v-if="tabCur==0" :src="activeIndex === index?def+item.url1:def+item.url" class="image" /> -->
						<image v-if="tabCur==1" :src="incomeActiveIndex === index?item.url1:item.url" class="image" />
						<text v-if="tabCur==0" :style="{color:activeIndex === index?'#1296db':'#707070'}" class="icon-item-text">{{ item.text }}</text>
						<text v-if="tabCur==1" :style="{color:incomeActiveIndex === index?'#1296db':'#707070'}" class="icon-item-text">{{ item.text }}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="keyboardbox" :class="is_ipX?'foot_bottom':''">
			<view class="numkeyboard" v-if="type===1">
				<view class="num-area">
					<view class="row" v-for="(item,index) in numKeybordList" :key="index">
						<view :class="['item',ite===0?'z':'',(disableDot && ite==='.')?'disabled':'']" v-for="(ite,idx) in item"
						 hover-class="active" :hover-start-time="0" :hover-stay-time="5" :key="idx" @tap="input(ite)">{{ite}}</view>
					</view>
				</view>
				<view class="btn-area">
					<view :class="['item','del']" hover-class="active" :hover-start-time="0" :hover-stay-time="5" @tap="deleteVal">
						删除
					</view>
					<view class="confirem item" hover-class="active" :hover-start-time="0" :hover-stay-time="5" @tap="confirm">
						完成
					</view>
				</view>
			</view>
		</view>
		<view class="safe-area" v-if="safeAreaInsetBottom"></view>
	</view>


</template>

<script>
	import {
		callCloudFunction,
		getUserInfo
	} from '../../../common/public_util.js';
	import uniCard from '../../../components/uni-ui/uni-card/uni-card.vue'

	let _self;
	export default {
		components: {
			uniCard
		},
		props: {
			income:{
				type:Array, 
				default: [
				],
				// observer
			},
			expend:{
				type: Array, 
				default:[
					
				]
			},
			billInfo:{
				type:Object, 
				default:{}
			},
				tabCur:{
					type: Number, // 1-数字键盘 2-身份证号  -车牌号
					default: 0
				},

			type: {
				type: Number, // 1-数字键盘 2-身份证号  -车牌号
				default: 1
			},
			safeAreaInsetBottom: { //是否设置安全区
				type: Boolean,
				default: false
			},
			disableDot: { //数字键盘是否禁止点击.仅type为number生效
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				col:"",
				option: {}, //参数
				id: null,
				incomeMoney: '0.00', //收入金额
				money: '0.00', //支出金额
				numKeybordList: [
					[1, 2, 3],
					[4, 5, 6],
					[7, 8, 9],
					[0, '.']
				],
				active: 1,
				incomeActiveIndex: 0,
				activeIndex: 0
				
			}
		},
		created() {
			_self = this;
			this.col=this.$parent.col
			let billInfo=this.billInfo
			this.tabCur=billInfo.type||0
			if (billInfo.type==0) {
				this.money=billInfo.money
				this.activeIndex=billInfo.active_id
			}else if (billInfo.type==1) {
				this.incomeMoney=billInfo.money
				this.incomeActiveIndex=billInfo.active_id

			}
			// uni.$on('test',function(data){
			// 	console.log('监听到事件来自 update ，携带参数 msg 为：' + data.msg);
			// })
		},
		beforeDestroy(){
			this.$parent.billInfo={}
		},
		methods: {
			test(){
				console.log(123123);
			},
			/**
			 * 完成
			 */
			async confirm() {
				let _self=this
				let money = _self.money; //支出
				if (_self.tabCur == 1) {
					money = _self.incomeMoney; //收入
				}
				let moneyNumber = new Number(money);
				if (moneyNumber <= 0) {
					uni.showToast({
						icon: "none",
						title: '请输入大于0的金额'
					});
					return;
				}
				let activeIndex=this.tabCur==0?this.activeIndex:this.incomeActiveIndex
				let id=this.billInfo.id
				let option={
					id,
					type:this.tabCur,
					money:this.tabCur==0?this.money:this.incomeMoney,
					active_item:this.tabCur==0?this.expend[activeIndex]:this.income[activeIndex],
					active_id:activeIndex
				}
				let res =await uni.$request(option,
				"bill/index/setbillinfo")
				console.log(option);
				// console.log(res);
				if (res.code==200) {
					await uni.$success(res.msg,1000)
					if (option.id) {
						this.$parent.billInfo={}
						this.$parent.cur=2
					} 
				}
				
				// console.log(params);
				
				
			},
			/**
			 * 删除
			 */
			deleteVal() {
				let _self=this
				let money = _self.money; //支出
				if (_self.tabCur == 1) {
					money = _self.incomeMoney; //收入
				}
				if (money != '0.00' && money.length > 0)
					money = money.substring(0, money.length - 1)
				if (money.length <= 0)
					money = '0.00';
				//支出
				if (_self.tabCur == 0) {
					_self.money = money;
				} else if (_self.tabCur == 1) { //收入
					_self.incomeMoney = money;
				}
			},
			/**
			 * 按键
			 * @param {Object}
			 */
			input(val) {
				console.log(this.incomeMoney);
				let money =this.tabCur==0?this.money:this.incomeMoney; //支出
				
				console.log(val);
				console.log(money);

				if (money == '0.00' && val != null) {
					money = val.toString();
				} else {
					let arr = money.split('.');
					if (val == '.' && arr.length > 1) {

					} else if (arr.length <= 1 || (arr.length > 1 && arr[1].length <= 1)) {
						if (money == '0' && val != '.') {
							money = val.toString();
						} else if (money != '0' || val != '0')
							money += val;
					}
				}
				//支出
				if (_self.tabCur == 0) {
					_self.money = money;
				} else if (_self.tabCur == 1) { //收入
					_self.incomeMoney = money;
				}

			},
			
			/**
			 * 图片点击
			 * @param {Object} index
			 */
			switchActive(index) {
				if (_self.tabCur == 1) {
					_self.incomeActiveIndex = index;
				} else if (_self.tabCur == 0) {
					_self.activeIndex = index;
				}
			},
		},
		

	}
	
</script>

<style>

	.image {
		width: 50rpx;
		height: 50rpx;
		/* background: #ffaaaa; */
		border-radius: 100rpx;
		padding:20rpx;
		box-sizing: content-box;
	}

	.text {
		font-size: 26rpx;
		margin-top: 10rpx;
	}

	.grid-item-box {
		flex: 1;
		/* position: relative;
	*/
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 15px 0;
	}
</style>

<style lang="scss" scoped>
	@import "../main.scss";
	.list{
		flex-wrap: wrap;
		.item{
			width: 20%;
		}
	}
	.keyboardbox {
		width: 100%;
		position: absolute;
		left: 0;
		bottom: 160rpx;
		background-color: #FFFFFF;

		.numkeyboard {
			height: 432rpx;
			display: flex;
			background-color: #ebedf0;

			.btn-area {
				width: 180rpx;
				height: 100%;
				display: flex;
				flex-direction: column;

				.item {
					width: 100%;
					display: flex;
					justify-content: center;
					align-items: center;
					flex-grow: 1;
				}

				.del {
					background-color: #ebedf0;
					color: #333;

					&.active {
						background-color: #f1f3f5;
					}
				}

				.confirem {
					background-color: #1989fa;
					color: #FFFFFF;

					&.active {
						background-color: #0570db;
					}
				}
			}

			.num-area {
				flex-grow: 1;
				display: flex;
				flex-wrap: wrap;

				.row {
					width: 100%;
					height: 25%;
					display: flex;
					margin-top: 1px;

					.item {
						flex-grow: 1;
						height: 100%;
						display: flex;
						justify-content: center;
						align-items: center;
						background-color: #FFFFFF;
						border-right: 1px solid #ebedf0;
						width: 33.33%;

						&.active {
							background-color: #ebedf0;
						}

						&.z {
							flex-grow: 2;
							width: 66.66%;
						}

						&.disabled {
							background: #FFFFFF;
							color: #B9B9B9;
						}
					}
				}

			}
		}
	}

	.safe-area {
		padding-bottom: 0rpx;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
	}

	.platenumber {
		background-color: #f5f5f5;

		.header {
			height: 76rpx;
			background-color: #FFFFFF;
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 28rpx;
			border-top: 1px solid #f5f5f5;

			&>view {
				padding: 0 45rpx;
				color: #00a7ea;
				height: 100%;
				display: flex;
				align-items: center;

				&.active {
					background-color: #ebedf0;
				}
			}
		}

		.main {
			height: 435rpx;

			.row {
				margin: 13rpx 0;
				display: flex;
				justify-content: center;
				align-items: center;

				.item {
					width: 56rpx;
					height: 94rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					background-color: #FFFFFF;
					border-radius: 6rpx;
					margin: 0 7rpx;
					font-size: 24rpx;

					&.active {
						background-color: #ebedf0;
					}

					&.img {
						background-color: #c2cacc;
						width: 94rpx;

						&.active {
							background-color: #ddd;
						}

						&>image {
							width: 49rpx;
							height: 48rpx;
						}
					}
				}
			}

		}
	}
</style>
